
import json
import datetime
import uuid
from top.wardyan.core.erp.erp import *
from top.wardyan.core.utils.common import *

class MqttMsg :
    # id :str = ""
    # timestamp : str = ""
    # topic : str = ""
    # message : str = ""
    # sender : str = ""
    # receiver : str = ""
    # business : int = 0
    # custcode : str = ""
    # data = None

    def __init__(self, id : str = None, custcode : str = ERP_CUSTCODE, timestamp : str = None, topic : str = None, message : str = None, sender : str = ERP_CUSTCODE, receiver : str = "", business : int = 0, data = None):
        global TOPIC
        if id is None or id == "" :
            id = str(uuid.uuid4())
        if custcode is None or custcode == "" :
            custcode = ERP_CUSTCODE
        if timestamp is None or timestamp == "":
            timestamp = current_timestamp()
        if sender is None or sender == "":
            sender = ERP_CUSTCODE
        # if receiver is None or receiver == "":
        #     receiver = TOPIC

        self.id = id
        self.custcode = custcode
        self.timestamp = timestamp
        self.topic = topic
        self.message = message
        self.sender = sender
        self.receiver = receiver
        self.business = business
        self.data = data
    
    def datetime_handler(self, x):
        if isinstance(x, datetime.datetime):
            return x.isoformat()
        raise TypeError("Unknown type")
    def tojsonstr(self):
        # 将对象的属性转换为字典
        data = self.__dict__
        # 将字典转换为 JSON 字符串
        return json.dumps(data, default=self.datetime_handler, ensure_ascii=False)